package com.fangzhi.controller;

import com.aliyun.oss.ClientException;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.fangzhi.mobel.enums.GlobalCodeEnum;
import com.fangzhi.utils.GlobalParam;
import com.fangzhi.utils.ResponseResult;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

@RestController
@RequestMapping("/file")
public class UploadController {

    @Value("${com.aliyun.endpoint}")
    private String endpoint;
    @Value("${com.aliyun.accessKeyId}")
    private String accessKeyId;
    @Value("${com.aliyun.accessKeySecret}")
    private String accessKeySecret;

    @PostMapping("/upload")
    @ResponseBody
    public ResponseResult fileUpload(@RequestParam("file") MultipartFile file) throws IOException, ClientException {
        String originalFilename = file.getOriginalFilename();
        String fileType = originalFilename.substring(originalFilename.lastIndexOf("."));
        if (!fileType.equalsIgnoreCase(".JPEG") && !fileType.equalsIgnoreCase(".PNG") && !fileType.equalsIgnoreCase(".JPG"))
            return ResponseResult.systemException(GlobalCodeEnum.GL_FAIL_9997);
        // 生成md5
        String name = UUID.randomUUID().toString();
        OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
        ossClient.putObject("weiyunfangzhi-image", "image/" + name + fileType, file.getInputStream());
        Map<String, String> resultMap = new HashMap<>();
        resultMap.put("url", GlobalParam.IMAGE_URL + "/image/" + name + fileType);
        resultMap.put("update_url", "/image/" + name + fileType);
        return ResponseResult.OK(resultMap);
    }

//    @PostMapping("/upload")
//    @ResponseBody
//    public ResponseResult upload(@RequestParam("file") MultipartFile file) throws Exception {
//        if (file != null) {
//            String property = System.getProperty("user.dir");
//            File saveFile = new File(property + "/avatar/");
//            if (!saveFile.isFile())
//                saveFile.mkdirs();
//
//            //判断文件类型是否正确
//            String originalFilename = file.getOriginalFilename();
//            String fileType = originalFilename.substring(originalFilename.lastIndexOf("."));
//            if (".xls".equalsIgnoreCase(fileType) || ".xlsx".equalsIgnoreCase(fileType)) {
//                saveFile = new File(property  + "/excel/" + "productTypeExcel." + fileType);
//                file.transferTo(saveFile);
//                return ResponseResult.OK();
//            }
//            String fileName = UUID.randomUUID() + "." + file.getContentType().substring(
//                    file.getContentType().lastIndexOf("/") + 1);    // 创建文件名称
//            saveFile = new File(property  + "/avatar/"+ fileName);
//            file.transferTo(saveFile);
//            Map<String, String> resultMap = new HashMap<>();
//            resultMap.put("url", GlobalParam.IMAGE_URL + fileName);
//            resultMap.put("update_url", fileName);
//            return ResponseResult.OK(resultMap);
//        }
//        throw new RuntimeException("上传文件失败");
//    }


    public static void main(String[] args) throws MalformedURLException {
        String path = "/Users/edy/edge-tts/edge-tts/";
        File file = new File(path);		//获取其file对象
        File[] fs = file.listFiles();	//遍历path下的文件和目录，放在File数组中
        for(File f:fs){					//遍历File[]数组
            URL url =  new URL("https://weiyunfangzhi-image.oss-cn-qingdao.aliyuncs.com");
        }
    }
}
